package com.wodsy.phoenix.scaffold;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Created by zcwc on 2015/3/19.
 */
public class Scaffold_web_index {


    private static Configuration cfg;

    static {
        //初始化FreeMarker配置
        //创建一个Configuration实例
        cfg = new Configuration(Configuration.VERSION_2_3_21);
        //设置FreeMarker的模版文件位置

        try {
            cfg.setDirectoryForTemplateLoading(new File(new Scaffold_web_index().getClass().getResource("/").getPath()+"/"));
            cfg.setDefaultEncoding("UTF-8");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /*public static void main(String[] args) {

        String focusdate = "[{'id':1,'picTitle':'pic title','picUrl':'http://127.0.0.1:8080/static/demoimg/2.jpg','rbg':'#cfa972','picHref':'http://www.baidu.com','picWeight':1,'picAlt':'pic中1 '}]"; 
        String newGoods = "[{'id':3,'goodsId':1,'goodsName':'手工干花项链坠子复古毛衣复古项链包包手工干花项链坠子复古毛衣复古项链包包','goodsPic':'/static/demoimg/pic1.jpg','weight':1,'recommendContent':'手工干花项链坠子复古毛衣复古项链，包包手工干花项链坠子。复古毛衣复古项链包包，手工干花项链坠子复古毛衣复古项链'}]";
        String hotGoods="[{'id':1,'goodsId':1,'goodsName':'手工干花项链坠子复古毛衣复古项链包包手工干花项链坠子复古毛衣复古项链包包','goodsPic':'/static/demoimg/pic1.jpg','goodsPrice':0,'weight':1},{'id':2,'goodsId':1,'goodsName':'手工干花项链坠子复古毛衣复古项链包包手工干花项链坠子复古毛衣复古项链包包','goodsPic':'/static/demoimg/pic1.jpg','goodsPrice':0,'weight':1}]"; 
        String customizePic = "{'id':3,'picUrl':'/static/images/order.jpg','picHref':'http://www.baidu.com','picAlt':'这个图片啊啊啊啊'}";
        String shopRec="{'id':1,'shopId':11,'shopName':'怡然舒居','shopKeeperId':1,'shopKeeperName':'张大壮','shopKeeperMotto':'文化就在生活中，民艺就是一种文化，在我们的生活中流传了几百上千年','shopKeeperUrl':'/static/demoimg/pic4.jpg','picBanner':'/static/demoimg/pic3.jpg','picHref':'http://www.baidu.cn'}";

        //建立数据模型
        HashMap root = new HashMap();
        //放入对应数据key value
        root.put("focusdate", focusdate);
        root.put("newGoods",newGoods);
        root.put("hotGoods",hotGoods);
        root.put("customizePic",customizePic);
        root.put("shopRec",shopRec);



        generateSourceFileFromTemplate("index.ftl","index3",root);

    }*/


    public static void generateSourceFileFromTemplate(String templateFileName,String generateFileName,HashMap root){
        //取得模版文件
        Template t = null;
        try {
            t = cfg.getTemplate(templateFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //合并数据模型和模版，并将结果输出到out中
        try {

        	//String rest = "D:\\workspace_luna\\phoenix-apps-static\\src\\main\\webapp\\html\\index3.js";
        	String rest = "D:\\workspace_luna\\phoenix-apps-static\\src\\main\\webapp\\html\\index4.html";
        	String ss = new Scaffold_web_index().getClass().getResource("/").getPath()+"/"+generateFileName+".js";
            Writer out = new OutputStreamWriter(new FileOutputStream(new File(rest)));
            System.out.println(rest);

            t.process(root, out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }

    }



}
